package br.com.dev.tcc.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import br.com.dev.tcc.R;
import br.com.dev.tcc.bean.Apolice;
import br.com.dev.tcc.bean.DelegateListener;
import br.com.dev.tcc.bean.ResponseData;
import br.com.dev.tcc.task.BuscaApoliceTask;
import br.com.dev.tcc.util.CpfUtil;

public class SearchProfileDialog extends Dialog {

	private OnBuscaApoliceListener listener;

	private EditText etCpf;
	private EditText etPlaca;
	private EditText etCor;

	public SearchProfileDialog(Context context,
			OnBuscaApoliceListener listener) {

		this(context);
		this.listener = listener;
	}

	public SearchProfileDialog(Context context) {

		super(context, R.style.DialogAppTheme);

		super.getWindow();
		super.requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.setContentView(R.layout.search_profile_dialog);

		findViews();
		setListeners();
	}

	private void findViews() {

		etCpf = (EditText) findViewById(R.id.etCpf);
		etCor = (EditText) findViewById(R.id.etCor);
		etPlaca = (EditText) findViewById(R.id.etPlaca);
		etPlaca.addTextChangedListener(new TextPlacaWatcher());
		etPlaca.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
	}

	private void setListeners() {

		findViewById(R.id.btSearch).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {

						if (isValid()) {

							final ProgressCustomDialog pd = new ProgressCustomDialog(
									getContext());
							pd.setMessage(R.string.buscando_apolice);
							pd.setCancelable(false);

							dismiss();

							pd.show();

							DelegateListener<ResponseData<Apolice>> delegate = new DelegateListener<ResponseData<Apolice>>() {

								@Override
								public void onDelegate(
										ResponseData<Apolice> data) {

									pd.dismiss();

									if (listener != null)
										listener.onComplete(data);
								}
							};

							new BuscaApoliceTask(getContext(), delegate,
									CpfUtil.getFormattedCpf(etCpf
											.getText().toString()), etPlaca
											.getText().toString(), etCor
											.getText().toString()).execute();
						}
					}
				});

		findViewById(R.id.btCancel).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {

						dismiss();
					}
				});
	}

	private boolean isValid() {

		boolean validCpf = CpfUtil.isValid(getContext(), etCpf);
		boolean validPlaca = isValidField(etPlaca, 8);
		boolean validCor = isValidField(etCor, -1);

		return validCpf && validPlaca && validCor;
	}

	private boolean isValidField(EditText editText, int size) {

		String text = editText.getText().toString().trim();

		if (text.length() > 0) {

			if (size == -1 || text.length() == size) {
				editText.setCompoundDrawablesWithIntrinsicBounds(null, null,
						null, null);
				return true;
			}
		}

		editText.setCompoundDrawablesWithIntrinsicBounds(
				null,
				null,
				getContext().getResources().getDrawable(R.drawable.exclamation),
				null);
		return false;
	}

	public void setListener(OnBuscaApoliceListener listener) {

		this.listener = listener;
	}

	public interface OnBuscaApoliceListener {

		public void onComplete(ResponseData<Apolice> response);
	}

	private class TextPlacaWatcher implements TextWatcher {

		@Override
		public void afterTextChanged(Editable s) {
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {

			if (s.length() < 3)
				etPlaca.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);

			if (count > before && s.length() == 3) {
				etPlaca.setText(s + "-");
				etPlaca.setInputType(InputType.TYPE_CLASS_NUMBER);
				etPlaca.setSelection(4);
			}
			else if(before > count && s.length() == 3){
				etPlaca.setText(s.toString().substring(0, s.length() - 1));
				etPlaca.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
				etPlaca.setSelection(s.length() - 1);
			}
		}
	}
}
